﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using ThuPhap.Models;
using System.Collections.ObjectModel;
using System.IO;

namespace ThuPhap.Class
{
    public class SaveTemplateInfo
    {
        public SaveTemplateInfo()
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.DirectoryExists("SavedTemplates") == false)//directory does not exist
            {
                //create SavedPictures directory
                storage.CreateDirectory("SavedTemplates");
            }
        }

        public ObservableCollection<Template> GetData()
        {
            var results = new ObservableCollection<Template>();
            string[] filenames = this.GetFileNames();

            foreach (var item in filenames)
            {
                var data = this.GetDataFile(item);
                results.Add(data);
                
            }
            return results;
        }

        public string[] GetFileNames()
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string[] filenames = storage.GetFileNames("SavedTemplates//*.*");
            if (filenames.Length > 0)
                Array.Sort(filenames, StringComparer.InvariantCulture);
            return filenames;
        }

        public Template GetDataFile(string FileName)
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string filename = "SavedTemplates\\" + FileName;
            //string filename = "SavedPictures\\" + FileName;
            if (storage.DirectoryExists("SavedTemplates") && storage.FileExists(filename))
            {
                using (var storageFile = storage.OpenFile(filename, System.IO.FileMode.Open))
                {
                    var xmlSerializer = new XmlSerializer(typeof(Template));

                    return (Template)xmlSerializer.Deserialize(storageFile);
                }
            }
            return null;
        }


        public ObservableCollection<Template> GetDefaultData()
        {
            var results = new ObservableCollection<Template>();
            var xmlSerializer = new XmlSerializer(typeof(Template));
            var uri = new Uri("WeddingTemplate.tpl", UriKind.Relative);
            var streamInfo = Application.GetResourceStream(uri);
            var stream = new StreamReader(streamInfo.Stream);
            results.Add((Template)xmlSerializer.Deserialize(stream));

            //var data = this.GetDataFile();
            //    results.Add(data);

            return results;
        }

        public string SavedNewTemplate(Template template)
        {
            string filename = template.Name;
            string result = string.Empty;
            int index = 1;
            bool flag = false;
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string name = string.Format("SavedTemplates//{0}", filename.Replace(' ', '_'));
            string finalName = name;
            //string name = string.Format("SavedPictures//{0}", filename);

            while (!flag)
            {
                if (storage.FileExists(finalName + ".tpl"))
                {
                    finalName = name + "_" + index++;
                }
                else
                {
                    flag = true;
                }
            }
            finalName = finalName + ".tpl";

            //if (storage.FileExists(string.Format("SavedPictures//{0}", filename)) == false)
            //{
            using (var storageFile = storage.CreateFile(finalName))
            {
                var xmlSerializer = new XmlSerializer(typeof(Template));
                xmlSerializer.Serialize(storageFile, template);
                storageFile.Close();
            }
            result = finalName;
            //}
            //else
            //{
            //    result = string.Empty;
            //}

            return result;
        }

        public string SavedNewTemplate(Stream stream)
        {

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Template));
            var template = xmlSerializer.Deserialize(stream) as Template;

            string filename = template.Name;
            string result = string.Empty;
            int index = 1;
            bool flag = false;
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string name = string.Format("SavedTemplates//{0}", filename.Replace(' ', '_'));
            string finalName = name;
            //string name = string.Format("SavedPictures//{0}", filename);
            while (!flag)
            {
                if (storage.FileExists(finalName + ".tpl"))
                {
                    finalName = name + "_" + index++;
                }
                else
                {
                    flag = true;
                }
            }
            finalName = finalName + ".tpl";

            //if (storage.FileExists(string.Format("SavedPictures//{0}", filename)) == false)
            //{
            using (var storageFile = storage.CreateFile(finalName))
            {
                xmlSerializer.Serialize(storageFile, template);
                storageFile.Close();
            }
            result = finalName;
            //}
            //else
            //{
            //    result = string.Empty;
            //}

            return result;
        }

        public void RemoveFile(string FileName)
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();
            string filename = "SavedTemplates\\" + FileName;
            //string filename = "SavedPictures\\" + FileName;
            if (storage.DirectoryExists("SavedTemplates") && storage.FileExists(filename))
            {
                storage.DeleteFile(filename);
            }
        }
    }
}
